package tz.preprocessing.bagOfWords;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class BagOfWords {

	private Map<String, Double> bag;
	LinkedList<String> words;
	/*stevilo vseh besed*/
	private int wordsInDoc;
	
	/**
	 * Creates a map (Bag of words) and calls method CalcBagOfWords.
	 * @param words
	 */
	public BagOfWords(LinkedList<String> words){
		bag = new TreeMap<String, Double>();
		wordsInDoc=words.size();
		CalcBagOfWords(words);
		this.words = words;
	}
	
	/**
	 * Calculates a bag of words.
	 * Method counts occurrences of each word in a list and 
	 * saves it in a Map of (words, occurrences).
	 * @param words
	 */
	private void CalcBagOfWords(LinkedList<String> words){
		double occurrences;
		for(String word: words){
			//List already contains a word.
			if(bag.containsKey(word)){
				occurrences = bag.get(word)+1;
				
			//First occurrence of a word.
			}else{
				occurrences=1;
			}
			bag.put(word, occurrences);	
		}
	}
	
	public List<String> getListOfWordsThatOccurrenceIsGreatherEqualThan(int x)
	{
		LinkedList<String> ll = new LinkedList<String>();
		for(String word : bag.keySet())
		{
			if(bag.get(word) >= x)
			{
				ll.add(word);
			}
		}
		return ll;
	}
	
	public Map<String, Double> getBag(){
		return bag;
	}
	
	public int getwordsInDoc(){
		return wordsInDoc;
	}
}
